var PortalMrlist = {};
$(function () {
    $.parser.parse(); // 解析整个页面
    var curDate = getNowFormatDate();
    Common_SetValue('SDate', curDate);
    Common_SetValue('EDate', curDate);
    $HUI.combobox('#DateType',
        {
            data: [
                {'DateType': 'InHosp', 'Desc': "入院日期"},
                {'DateType': 'OutHosp', 'Desc': "出院日期"}
            ],
            valueField: 'DateType',
            textField: 'Desc',
        })
    Common_SetValue('DateType', 'InHosp');
    $HUI.combobox('#Hospital',
        {
            data: $.parseJSON($("#officeMap").val()),
            valueField: 'value',
            textField: 'text',
            onSelect: PortalMrlist.officeChange,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                var val = $(this).combobox("getData");
                if (val.length == 2) {//如果就一个院区 加上全部=2
                    $(this).combobox("setValue", val[1].value);
                    PortalMrlist.officeChange(val[1]);
                    $("#oneOffice").val("yes");
                } else {
                    $(this).combobox("setValue", "");
                    $("#oneOffice").val("no");
                }
            }
        })
    $HUI.combobox('#LocDic',
        {
            data: $.parseJSON($("#departMap").val()),
            valueField: 'value',
            textField: 'text',
            defaultFilter: 6
        })
    $HUI.combobox('#QCDic',
        {
            data: $.parseJSON($("#entityMap").val()),
            valueField: 'value',
            textField: 'text',
            defaultFilter: 6
        })
    $('#search').on('click', function () {
        $('#home').layout('collapse', 'north');
        PortalMrlist.initMrlist(1)
    });
    $('#backQC').on('click', function () {
        $('#QCPanel').show();
        $('#MrListPanel').hide('');
        $('#backQC').hide();
    })
    var QCRows = $("#dataId").val();
    var data = JSON.parse(QCRows);
    PortalMrlist.initMrlist(data);
});

PortalMrlist.officeChange = function (that) {
    var ajax = new $ax(Sys.backendPath + "/sds/common/deptListByHospId", function (data) {
        $('#LocDic').combobox({
            data: data,
            valueField: 'value',
            textField: 'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    }, function (data) {
        layer.msg("查询失败!" + data.responseJSON.message + "!", {icon: 2});
    });
    ajax.set({"hospID": that.value, "id": "depart"});
    ajax.start();
}


PortalMrlist.showPatient = function (qcid,qcDisName) {
    // 处理父子界面的显示/隐藏
    $('#QCPanel').hide();
    $('#MrListPanel').show();
    $('#backQC').show();

    // 初始化病例表格
    PortalMrlist.QCMrListSub= $HUI.datagrid("#QCMrListSub",{
        fit:true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped:true,
        rownumbers:true,
        loadMsg:'数据加载中...',
        pageSize: 10,
        pageList : [10,50],
        idField:"id",
        columns:[[
            {field:'patName',title:'患者姓名',width:'160',align:'center',sortable:false},
            {field:'mrNo',title:'病案号',width:'160',align:'center',sortable:false},
            {field:'deptName',title:'科室',width:'160',align:'center',sortable:false},
            {field:'docName',title:'主管医生',width:'160',align:'center',sortable:false},
            {field:'description',title:'病种',width:'250',align:'center',sortable:false},
            {field:'admDate',title:'入院日期',width:'210',align:'center',sortable:false},
            {field:'disDate',title:'出院日期',width:'210',align:'center',sortable:false}
        ]]
        ,onDblClickRow:function(rowIndex,rowData){
            if(rowIndex>-1){
                PortalMrlist.gridQCMrList_onDbselect(rowData);
            }
        }
        ,onLoadSuccess:function(rowData){
        }
    });

    // 获取传入病例ID串信息
    //获取主页底部表格数据
    var params = {};
    var ajax = new $ax(Sys.backendPath + "/analysis/qcportal/selectPatDetail", function (data) {
        //加载表格数据
        $('#QCMrListSub').datagrid({loadFilter:pagerFilter}).datagrid('loadData', data);
    }, function (data) {
    });
    params.qcid = qcid;
    params.qcDisName=qcDisName
    ajax.set(params);
    ajax.start();
}




PortalMrlist.initMrlist = function (data) {
    if (data == 1) {
        //检索框查询
        var params = {};
        var ajax = new $ax(Sys.backendPath + "/analysis/qcportal/selectQCCategoryByCondition", function (rs) {
            data = rs;
        }, function (rs) {
        });
        params.aHospID = Common_GetValue('Hospital');
        params.aLocID = replaceValue(Common_GetValue('LocDic'));
        params.aLocDesc = replaceValue(Common_GetText('LocDic'));
        params.aQCEntity = replaceValue(Common_GetText('QCDic'));
        params.aDateFrom = Common_GetValue('SDate');
        params.aDateTo = Common_GetValue('EDate');
        params.aDateType = Common_GetValue('DateType');
        ajax.set(params);
        ajax.start();
    }
    //根据报告信息+查询参数，计算病种展现信息
    var QCRows = data.qcCategory
    var QCLength = QCRows.length, columns = 4, listHtml = "<div>"
    var per = Math.round((1 / columns) * 90) + '%';   //每列所在百分比
    var count = parseInt(QCLength / columns) + 1;
    for (var index = 0; index < count; index++) {		//行遍历
        var listlen = (((index + 1) * columns) < QCLength) ? (index + 1) * columns : QCLength;
        listHtml += "<div style='padding:10px'>";
        for (var qcIndex = index * columns; qcIndex < listlen; qcIndex++) { //对应行数据输出
            var qcobj = QCRows[qcIndex];
            listHtml += " <div style='border:1px solid #ccc;border-radius:5px;margin-right:10px;padding:3px;text-align:left;float:left;width:" + per + "' >"
            listHtml += " <p style='margin: 5px 0 10px 0;font-weight:bold;'>" + " " + qcobj.description + "</p>";
            listHtml += "<div>"
            listHtml += "<img style='float: left;border-radius: 10px;'  src='/static/common/img/sds/" + qcobj.patName + ".png' width='70px' height='70px' />"
            listHtml += " <div style='float: left;margin-left: 15px;'>";
            listHtml += " <p style='margin:0 0 10px 0;'>质控触发人次数：<a style='cursor:pointer' onclick='PortalMrlist.qcDetail(\"" + qcobj.ids + "\",\"" + qcobj.description + "\");'><span>" + qcobj.count + "</span></a></p>";
            listHtml += " <p style='margin:0 0 10px 0;'>质控触发例数：<a style='cursor:pointer' onclick='PortalMrlist.showPatient(\"" + qcobj.ids + "\",\"" + qcobj.description + "\");'><span>" + qcobj.patCount + "</span></a></p>";
            listHtml += " </div>";
            listHtml += "</div>"
            listHtml += "</div>";
        }
        listHtml += "</div>"
        listHtml += "<div style='clear:both'></div>"
    }
    listHtml += "</div>"
    $('#QCMrList').html(listHtml);
    $.parser.parse('#QCMrList');  //解析元素样式
}


//赋值
function Common_SetValue() {
    var itmValue = '';

    var val = arguments[1];
    var txt = arguments[2];

    if (typeof (arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#' + arguments[0]);
    if ($this.length < 1) return '';
    if (typeof val == 'undefined') val = '';
    if (typeof txt == 'undefined') txt = '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val(val);
    } else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val(val);
    } else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('setValue', val);
    } else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('setValue', val);
    } else if (className == 'hisui-combobox') {  //下拉框
        if (val != "" && txt == "") {
            itmValue = $this.combobox('setValue', val);
        } else {
            itmValue = $this.combobox('setValue', txt);
        }
    } else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('setValue', val);
    } else if (className == 'hisui-checkbox') {  // 单个复选框
        if (val == '') val = 0;
        itmValue = $this.checkbox('setValue', (val) ? true : false);
    } else if (className == 'hisui-radio') {  //单个单选框
        if (val == '') val = 0;
        itmValue = $this.radio('setValue', (val) ? true : false);
    } else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('setValue', val);
    }

    return itmValue;
}

//取值
function Common_GetValue() {
    var itmValue = '';

    if (typeof (arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#' + arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    } else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    } else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    } else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    } else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    } else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    } else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    } else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    } else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {pageNumber: pageNum, pageSize: pageSize});
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}

//取值
function Common_GetText() {
    var itmValue = '';

    if (typeof (arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#' + arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    } else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    } else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    } else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    } else if (className == 'hisui-combobox') {  //下拉框
        itmValue = $this.combobox('getText');
    } else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    } else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.attr("label");
    } else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.attr("label");
    } else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

//值为全部替换为空
function replaceValue(text) {
    if ("全部" == text) {
        return ""
    } else {
        return text;
    }
}

PortalMrlist.qcDetail = function (id, qcDisName) {

    var index = Sys.open_c('质控项触发统计', '/analysis/qcportal/qcDetail?id=' + id + '&qcDisName=' + qcDisName, ['80%', '80%'], function () {
    });
    this.layerIndex = index;
}

